package cn.cloudreal.cbms.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import cn.cloudreal.cbms.vo.Organiza;
import cn.cloudreal.cbms.vo.Purview;
import cn.cloudreal.cbms.vo.UserInfo;

public final class Constants
{

    private Constants()
    {

    }

    /**
     * 用户鉴权KEY
     */
    public static final String SESSIONPRKEY = "logUser";

    /**
     * 查看页面
     */
    public static final String IS_VIEW = "1";

    /**
     * 修改页面初始化
     */
    public static final String IS_EDIT = "2";

    /**
     * 修改确认
     */
    public static final String IS_UPDATE = "3";

    /**
     * 新增确认
     */
    public static final String IS_ADD = "4";

    /**
     * 每页显示条数
     */
    public static final int PAGE_SIZE = 10;

    /**
     * 常量 0
     */
    public static final int CONST_0 = 0;

    /**
     * 常量 字符串 0
     */
    public static final String CONST_STR_0 = "0";

    /**
     * 常量 字符串 1
     */
    public static final String CONST_STR_1 = "1";

    /**
     * 常量 1
     */
    public static final int CONST_01 = 1;
    /**
     * 常量 2
     */
    public static final int CONST_02 = 2;
    /**
     * 常量 3
     */
    public static final int CONST_03 = 3;
    /**
     * 常量 4
     */
    public static final int CONST_04 = 4;
    /**
     * 常量 5
     */
    public static final int CONST_05 = 5;
    /**
     * 常量 6
     */
    public static final int CONST_06 = 6;
    /**
     * 常量 7
     */
    public static final int CONST_07 = 7;

    /**
     * 节点类型：根节点为0
     */
    public static final String NODE_TYPE_ROOT = "0";

    /**
     * 节点类型：枝干节点为1
     */
    public static final String NODE_TYPE_BRANCH = "1";

    /**
     * 节点类型：叶子节点为2
     */
    public static final String NODE_TYPE_LEAF = "2";

    /**
     * 常量空字符串 add Gao_xuejiao
     */
    public static final String STR_EMPTY = "";

    /**
     * 传播体验表的 类型区分 ="temp" 为临时表信息 add Gao_xuejiao
     */
    public static final String STR_EXPERIENCE_TYPE = "temp";

    /**
     * 设置utf-8 add Gao_xuejiao
     */
    public static final String STR_UTF = "UTF-8";

    /**
     * 设置 text/html; charset=utf-8 add Gao_xuejiao
     */
    public static final String STR_CHARSET = "text/html; charset=utf-8";

    /**
     * 节点动作类型：0不变
     */
    public static final String NODE_ACTION_TYPE_DEFAULT = "0";

    /**
     * 节点动作类型：1插入
     */
    public static final String NODE_ACTION_TYPE_INSERT = "1";

    /**
     * 节点动作类型：2修改
     */
    public static final String NODE_ACTION_TYPE_UPDATE = "2";

    /**
     * 节点动作类型：3移动
     */
    public static final String NODE_ACTION_TYPE_MOVE = "3";

    /**
     * 10086 渠道编码 add gao_xuejiao 02.25
     */
    public static final String CHANNEL_003 = "10086";

    /**
     * 8866 渠道编码 add gao_xuejiao 02.25
     */
    public static final String CHANNEL_002 = "8866";

    /**
     * e_mail 渠道编码 add gao_xuejiao 02.25
     */
    public static final String CHANNEL_001 = "e_mail";

    /**
     * 渠道数量最大上限值 add gao_xuejiao 02.27
     */
    public static final String CHANNEL_MAX_VALUE = "channel_max";

    /**
     * 时间格式化字符串 add gao_xuejiao 02.27
     */
    public static final String TIME_FORMAT = "yyyy-MM";

    /**
     * 常量 "-" 字符
     */
    public static final String CONST_STR_H = "-";

    /**
     * 渠道编码前缀
     */
    public static final String CHANNEL_BEFORCODE = "code_";
    /**
     * 渠道编码后缀
     */
    public static final String CHANNEL_BEHINDCODE = "10001";

    /**
     * 规则属性类型：int
     */
    public static final String RULE_ATTRIBUTE_TYPE_INT = "int";

    /**
     * 规则属性类型：String
     */
    public static final String RULE_ATTRIBUTE_TYPE_String = "String";

    /**
     * 时间格式化字符串 精确到分 add gao_xuejiao 02.27
     */
    public static final String TIME_FORMANT_MIN = "yyyy-MM-dd hh:mm:ss";

    /**
     * 无法正常传播渠道 字符串'无法传播' add gao_xuejiao 02.27
     */
    public static final String CHANNEL_NO_PROPAGATION_STR = "";

    /**
     * 项目类型数据字典常量 add by tianxu
     */
    public static final String PROJECT_TYPE_CLASS_CODE = "XMLX";

    /**
     * 项目类型数据字典常量 add by tianxu
     */
    public static final String CITY_AREA_CODE = "CITY";
    
    /**
     * 媒介使用情况数据字典常量 add by tianxu
     */
    public static final String MEDIUM_STATE_CODE = "MJSYQK";
    

    /**
     * 角色操作范围
     */
    public static final String ROLE_RANGE = "JSCZFW";

    /**
     * 媒介使用情况_完成投放
     */
    public static final String MJSYQK_4 = "4";

    /**
     * 媒介使用情况_完成结算
     */
    public static final String MJSYQK_5 = "5";
    
    /**
     * 媒介使用情况_已中止
     */
    public static final String MJSYQK_6 = "6";

    /**
     * @throws DomainServiceException
     * @file:文件域列表
     * @fileName:上传文件的文件名
     * @path:文件上传的目录，相对目录。 说明：文件上传所存放目录的规则：
     * @返回结果：一个存放上传文件所在位置相对路径的ArrayList
     */
    public static void upload(File file, String fileName, String path)
            throws IOException
    {
        InputStream is = null;
        OutputStream os = null;
        try
        {
            File dir = new File(path);
            if (!dir.exists())
            {
                dir.mkdirs();
            }
            is = new FileInputStream(file.toString());

            String name = fileName.trim().toString();// 得到上传文件的原名称
            File destFile = new File(path, name);

            os = new FileOutputStream(destFile);

            byte[] buffer = new byte[400];

            int length = 0;

            while ((length = is.read(buffer)) > 0)
            {
                os.write(buffer, 0, length);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            is.close();
            os.close();
        }
    }

    /**
     * 获取生成ztree 组织架构字符串
     * 
     * @return
     */
    public static String getOrganizaTree(List<Organiza> organizaList)
    {
        String tempStr = null;
        StringBuffer ss = new StringBuffer("[");
        for (int i = 0; i < organizaList.size(); i++)
        {
            // 获取集合单独对象
            Organiza org = new Organiza();
            org = organizaList.get(i);

            // 如果当前是父节点打开
            if (org.getParentOrgCode().equals("-1"))
            {
                ss.append("{ id:'" + org.getOrgCode() + "',  pId:"
                        + org.getParentOrgCode() + ",   name:'"
                        + org.getOrgName() + "',open:true},");
            }
            else
            {
                ss.append("{ id:'" + org.getOrgCode() + "',  pId:"
                        + org.getParentOrgCode() + ",   name:'"
                        + org.getOrgName() + "'},");
            }
        }
        if (null != organizaList && organizaList.size() > 0)
        {
            tempStr = ss.substring(0, ss.toString().length() - 1).toString();
        }
        else
        {
            tempStr = ss.toString();
        }
        tempStr += "]";
        return tempStr;
    }

    /**
     * 判断当前 按钮操作是否可用
     * 
     * @param operateURL
     *            操作按钮请求的url 地址
     * @param userInfo
     *            当前操作用户对象 session 中获取
     * @return boolean true 可以操作 false 无操作权限
     */
    public static boolean isOperation(String operateURL, UserInfo userInfo)
    {
        if (null == operateURL || null == userInfo)
        {
            return false;
        }

        boolean flag = false;
        // 获取用户对象当前的所有按钮类型请求 权限对象
        List<Purview> purList = userInfo.getPurviewList();
        if (null != purList && purList.size() > 0)
        {
            // 循环判断
            for (int i = 0; i < purList.size(); i++)
            {
                Purview tempP = new Purview();
                tempP = purList.get(i);

                // 如果url 地址不为null
                if (null != tempP.getActionUrl())
                {
                    // 如果权限类型也不为null 0 菜单类型 1 按钮类型
                    if (null != tempP.getNodeType())
                    {
                        if (tempP.getNodeType().equals("1")
                                && tempP.getActionUrl().equals(operateURL))
                        {
                            flag = true;
                            break;
                        }
                    }
                }

            }
        }
        return flag;
    }

}
